﻿@using KRM.Core
@using KRM.Web.Models
@model KRM.Core.tblTicket
@{
    IEnumerable<vWebTicketAudit> activities = ViewBag.ListActivities;
    var lstComment = new List<CommentTicket>();
    foreach (var item in Model.Comments)
    {
        var filenames = item.EmailAttachments.Select(a => a.Filename).ToList();
        lstComment.Add(new CommentTicket()
        {
            Avatar = item.SentByUser == null ? "avatar-default.jpg" : item.SentByUser.Avatar ?? "avatar-default.jpg",
            Type = "Comment",
            IsIncommingEmail = item.IsIncommingEmail,
            IsNoteComment = item.IsNoteComment,
            Username = item.SentByUser == null ? item.From : item.SentByUser.Username,
            Date = item.Date,
            TicketId = Model.TicketID,
            SentByUserID = item.SentByUserID,
            EmailID = item.EmailID,
            Body = item.Body,
            Filename = filenames
        });
    }
    foreach (var item in activities)
    {
        lstComment.Add(new CommentTicket()
        {
            Username = item.Username,
            Body = item.Action,
            Date = item.Date,
            Type = "Log"
        });
    }
}
@foreach (var item in lstComment.OrderByDescending(p => p.Date))
{
    if (item.Type.Equals("Comment"))
    {
    <div class="row-fluid ticket-comment-row">
        <div class="span1">
            <img alt="" src="/Upload/Avatar/@item.Avatar" onError="this.src='/upload/avatar/avatar-default.jpg';" style="text-align: center; height: 40px; max-width: 40px; background-color: #ddd !important;">
        </div>
        <div class="span11">
            @{
        var commentClass = "";
        commentClass = item.IsIncommingEmail == true ? "ticket-comment-incomeing" : (item.IsNoteComment == true ? "ticket-comment-note" : "ticket-comment-outgoing");
            }
            <div class="row-fluid">
                <strong>@item.Username</strong>
                <span class="ticket-comment-time">
                    @item.Date
                    @if (item.IsIncommingEmail != true)
                    {
                        <a onclick="$('#frViewTicket_@Model.TicketID #AssignToUserId').val(@item.SentByUserID)" style="cursor: pointer;">(assign)</a>
                    }
                    else
                    {
                        <a onclick="linkmailtootherticket(@item.EmailID,@item.TicketId)" style="cursor: pointer;">(Link to other ticket)</a>
                    }
                </span>
            </div>
            <div class="ticket-comment @commentClass">@Html.Raw(item.Body)</div>
            <div class="ticket-comment-attachment">
                <ul>
                    @foreach (var attach in item.Filename)
                    {
                        <li><a target="_blank" href="\Ticket\GetFileAttachment?path=@attach">@attach.Split('\\').Last()</a></li>
                    }
                </ul>
            </div>
        </div>
    </div>
    }
    else
    {
    <div class="row-fluid ticket-comment-row">
        <div class="span12" style="padding: 5px; background-color: #d1e5f8;">
            @item.Username <strong>@item.Body</strong> on @string.Format("{0:hh:mm, dd/MM/yyyy}", item.Date)
        </div>
    </div>
    }

}